"""
Case Type   : Postgis
Case Name   : 拷贝postgis相关动态库和配置文件适配功能，创建postgis_raster、postgis_topology
Create At   : 2024/7/29
Owner       : peilinqian
Description :
    1、拷贝postgis相关动态库和配置文件至对应路径下
    2、创建postgis_raster、postgis_topology
Expect      :
    1、相应文件拷贝成功
    2、创建postgis_raster、postgis_topology，创建扩展成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class PostgisTest(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} 开始执行-----')
        self.common = Common()
        self.sh_primysh = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.root_node = Node('PrimaryRoot')
        self.constant = Constant()
        self.file_path1 = os.path.join(macro.DB_INSTANCE_PATH, '../app/lib')
        self.file_path2 = os.path.join(macro.DB_INSTANCE_PATH,
                                       '../app/share/postgresql/extension')
        self.file_path3 = os.path.join(self.file_path1, 'postgresql')
        self.postgis_path = os.path.join(macro.FTP_PLUGINS_PATH, 'postgis')
        self.postgis_x86 = os.path.join(self.postgis_path, 'plat_X86')
        self.postgis_arm = os.path.join(self.postgis_path, 'plat_ARM')

        platform = ['x86_64', 'aarch64']
        cmd = 'arch'
        res = self.user_node.sh(cmd).result()
        self.src_file_path = self.postgis_x86 if res.strip() == platform[
            0] else self.postgis_arm
        self.log.info(self.src_file_path)

    def test_postgis(self):
        file_list = ['libjson-c.so.2', 'libgeos_c.so.1', 'libproj.so.9',
                     'libgeos-3.6.2.so', 'libgdal.so.1.18.0',
                     'liblwgeom-2.4.so.0', 'postgis.control',
                     'postgis--2.4.2.sql', 'postgis_raster--2.4.2.sql',
                     'postgis_raster.control', 'postgis_topology--2.4.2.sql',
                     'postgis_topology.control', 'postgis-2.4.so',
                     'rtpostgis-2.4.so', 'postgis_topology-2.4.so']

        text = '---step1:拷贝相关动态库和配置文件到数据库对应路径下   expect:成功---'
        self.log.info(text)
        for i, file in enumerate(file_list[0:15]):
            text = f'---copy {file}文件---'
            self.log.info(text)
            if i < 6:
                file_path = self.file_path1
            elif i < 12:
                file_path = self.file_path2
            else:
                file_path = self.file_path3
            copy_cmd = f'wget -N -P {file_path} ' \
                       f'{self.src_file_path}/{file};'
            self.log.info(copy_cmd)
            copy_res = self.root_node.sh(copy_cmd).result()
            self.log.info(copy_res)
            cat_cmd = f'ls {file_path} | grep {file}'
            self.log.info(cat_cmd)
            cat_res = self.root_node.sh(cat_cmd).result()
            self.log.info(cat_res)
            self.assertIn(file, cat_res, '执行失败' + text)

        text = '---重命名libgdal.so.1.18.0---'
        self.log.info(text)
        mv_cmd = f"mv {os.path.join(self.file_path1, 'libgdal.so.1.18.0')} " \
                 f"{os.path.join(self.file_path1, 'libgdal.so.1')}"
        self.log.info(mv_cmd)
        mv_res = self.root_node.sh(mv_cmd).result()
        self.log.info(mv_res)

        chmod_cmd = f'chown -R {self.user_node.ssh_user}:' \
                    f'{self.user_node.ssh_user} {self.file_path1};' \
                    f'chown -R {self.user_node.ssh_user}:{self.user_node.ssh_user} ' \
                    f'{self.file_path2};' \
                    f'chown -R {self.user_node.ssh_user}:{self.user_node.ssh_user} ' \
                    f'{self.file_path3};'
        self.log.info(chmod_cmd)
        chmod_res = self.root_node.sh(chmod_cmd).result()
        self.log.info(chmod_res)

        text = '---step2:创建postgis_raster、postgis_topology; expect:成功---'
        self.log.info(text)
        sql_cmd = "drop extension if exists postgis cascade;" \
                  "create extension postgis;" \
                  "create extension postgis_raster;" \
                  "set behavior_compat_options='bind_procedure_searchpath';" \
                  "create extension postgis_topology;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primysh.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertEqual(3, sql_res.count(
            self.constant.create_extension_success), '执行失败' + text)

    def tearDown(self):
        self.log.info('------No need to clean------')
        self.log.info(f'-----{os.path.basename(__file__)} 执行结束-----')
